package com.cqyc.shop.controller;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cqyc.shop.domain.Cart;
import com.cqyc.shop.domain.Shop;
import com.cqyc.shop.domain.User;
import com.cqyc.shop.services.CartServices;


@WebServlet(urlPatterns="/showCart.do")
public class ShowCartServlet extends HttpServlet{
	private static final long serialVersionUID = 8880201242797888881L;
	private Logger logger = LoggerFactory.getLogger(getClass());
	private CartServices cartServices = new CartServices();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug("购物车保存显示表演开始");
		try {
			//1:从前台获取参数
			String num = request.getParameter("num");
			logger.debug("{}",num);
			
			Integer num2 = Integer.valueOf(num);
			HttpSession session = request.getSession();
			Object ouser = session.getAttribute("user");
			User user = (User)ouser;
			logger.debug("{}",user.getUsercode());
			Object  shop2 = session.getAttribute("shop2");
			Shop shop =(Shop)shop2;
			logger.debug("{}",shop.getShopname());
			Cart cart = new Cart();
			cart.setUsercode(user.getUsercode());
			cart.setShopname(shop.getShopname());
			cart.setCartimg(shop.getShopimg());
			cart.setCartinfo(shop.getShopinfo());
			cart.setCartdate(new Date());
			cart.setCartauthor(shop.getProduce());
			cart.setPrice(shop.getPrice());
			cart.setHowmany(num2);
			cart.setBuytotal(shop.getShoptotal());
			//2:数据转换
			//3:数据校验
			//4:业务处理  
			 cartServices.findPager(cart);
        	cartServices.save(cart);
			logger.debug("购物车已经保存成功");
			request.setAttribute("sucess", "成功添加到购物车中");
			//5:跳转
			request.getRequestDispatcher("/Adminadd.jsp").forward(request, response);
		} catch (Exception e) {
			logger.debug("{}",e.getMessage());
			request.setAttribute("error", e.getMessage());
			request.getRequestDispatcher("/Adminadd.jsp").forward(request, response);
		}
	}
	
	
}
